package com.vancouver.knows.business.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.vancouver.knows.business.bean.MyArticleInfo;
import com.vancouver.knows.business.bean.MyAtcommentsInfo;
import com.vancouver.knows.business.bean.MyCommentsInfo;
import com.vancouver.knows.business.bean.MyFollowedInfo;

/**
 * 我的文章，我的关注，我的评论@我的评论 列表解析
 * Created by llf
 */
public class MyArticleHandle extends ACheckableJsonParser {

	public static final String ARTICLE_MAP = "my_article_map";

	public static final String USER_POSTED_ARTICLES = "user_posted_articles"; // 我的文章
	public static final String USER_FOLLOWED_ITEMS = "user_followed_items"; // 我的关注
	public static final String USER_POSTED_COMMENTS = "user_posted_comments"; // 我的评论
	public static final String USER_MENTIONED_COMMENTS = "user_mentioned_comments"; // @我的评论
	private String pageType = null;
	private HashMap<String, Object> resultMap = new HashMap<String, Object>();

	@Override
	protected void parserContent(JSONObject content) throws Exception {
		String returnData = null;
		switch (pageType) {
		case USER_POSTED_ARTICLES:
			returnData = content.getString("post_details");
			if (returnData == null || returnData.equals("[]")) {
				resultMap.put(ARTICLE_MAP, new ArrayList<MyArticleInfo>());
				return;
			}
			List<MyArticleInfo> myArticleList = null;
			List<MyArticleInfo> myArticleInfoList = JSON.parseArray(returnData, MyArticleInfo.class);
			if (myArticleInfoList == null) {
				myArticleInfoList = new ArrayList<MyArticleInfo>();
				resultMap.put(ARTICLE_MAP, myArticleInfoList);
			}else{
				myArticleList = removeArticles(myArticleInfoList);
				resultMap.put(ARTICLE_MAP, myArticleList);
			}
			break;
		case USER_FOLLOWED_ITEMS:
			returnData = content.getString("item_list");
			if (returnData == null || returnData.equals("[]")) {
				resultMap.put(ARTICLE_MAP, new ArrayList<MyFollowedInfo>());
				return;
			}
			List<MyFollowedInfo> myFollowedList = JSON.parseArray(returnData, MyFollowedInfo.class);
			if (myFollowedList == null) {
				myFollowedList = new ArrayList<MyFollowedInfo>();
			}
			resultMap.put(ARTICLE_MAP, myFollowedList);

			break;
		case USER_POSTED_COMMENTS:
			returnData = content.getString("comment_details");
			if (returnData == null || returnData.equals("[]")) {
				resultMap.put(ARTICLE_MAP, new ArrayList<MyCommentsInfo>());
				return;
			}
			List<MyCommentsInfo> myCommentsList = null;
			List<MyCommentsInfo> myCommentsInfoList = JSON.parseArray(returnData, MyCommentsInfo.class);
			if (myCommentsInfoList == null) {
				myCommentsInfoList = new ArrayList<MyCommentsInfo>();
				resultMap.put(ARTICLE_MAP, myCommentsInfoList);
			} else {
				// 如果"comment_approved":"trash"，表示已经删除的，不需要加入
				myCommentsList = removeDetailCommentsInfo(myCommentsInfoList);
				resultMap.put(ARTICLE_MAP, myCommentsList);
			}

			break;
		case USER_MENTIONED_COMMENTS:
			returnData = content.getString("comment_details");
			if (returnData == null || returnData.equals("[]")) {
				resultMap.put(ARTICLE_MAP, new ArrayList<MyAtcommentsInfo>());
				return;
			}
			List<MyAtcommentsInfo> myAtCommentsList = JSON.parseArray(returnData, MyAtcommentsInfo.class);
			if (myAtCommentsList == null) {
				myAtCommentsList = new ArrayList<MyAtcommentsInfo>();
			} else {
				// 如果"comment_approved":"trash"，表示已经删除的，不需要加入
				myAtCommentsList = removeMyAtCommentsInfo(myAtCommentsList);
			}
			resultMap.put(ARTICLE_MAP, myAtCommentsList);
			break;

		default:
			break;
		}
	}

	public HashMap<String, Object> getResultMap() {
		return resultMap;
	}

	public void saveCurrentPageType(String page) {
		this.pageType = page;
	}


	private List<MyArticleInfo> removeArticles(List<MyArticleInfo> articleList) {
		for (int i = 0; i < articleList.size(); i++) {
			if ("trash".equals(articleList.get(i).getPostStatus())) {
				articleList.remove(i);
				i--;
			}
		}
		return articleList;
	}

	private List<MyCommentsInfo> removeDetailCommentsInfo(List<MyCommentsInfo> myCommentsList) {
		for (int i = 0; i < myCommentsList.size(); i++) {
			if ("trash".equals(myCommentsList.get(i).getcApproved())) {
				myCommentsList.remove(i);
				i--;
			}
		}
		return myCommentsList;
	}

	private List<MyAtcommentsInfo> removeMyAtCommentsInfo(List<MyAtcommentsInfo> myAtCommentsList) {
		for (int i = 0; i < myAtCommentsList.size(); i++) {
			if ("trash".equals(myAtCommentsList.get(i).getcApproved())) {
				myAtCommentsList.remove(i);
				i--;
			}
		}
		return myAtCommentsList;
	}
}